package cn.iocoder.yudao.module.dr.controller.admin.sourceapply;

import cn.iocoder.yudao.module.dr.controller.admin.source.vo.SourceCatalogWithSourceDataRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapply.vo.SourceApplyPageReqVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapply.vo.SourceApplyRespVO;
import cn.iocoder.yudao.module.dr.controller.admin.sourceapply.vo.SourceApplySaveReqVO;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourceapplydetail.SourceApplyDetailDO;
import cn.iocoder.yudao.module.dr.service.source.SourceService;
import cn.iocoder.yudao.module.dr.service.sourceapplydetail.SourceApplyDetailService;
import org.springframework.web.bind.annotation.*;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import jakarta.validation.*;
import jakarta.servlet.http.*;
import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.dr.controller.admin.sourceapply.vo.*;
import cn.iocoder.yudao.module.dr.dal.dataobject.sourceapply.SourceApplyDO;
import cn.iocoder.yudao.module.dr.service.sourceapply.SourceApplyService;

@Tag(name = "管理后台 - 资源申请")
@RestController
@RequestMapping("/dr/source-apply")
@Validated
public class SourceApplyController {

    @Resource
    private SourceApplyService sourceApplyService;

    @Resource
    private SourceApplyDetailService sourceApplyDetailService;

    @Resource
    private SourceService sourceService;

    @PostMapping("/create")
    @Operation(summary = "创建资源申请")
    @PreAuthorize("@ss.hasPermission('dr:source-apply:create')")
    public CommonResult<Long> createSourceApply(@Valid @RequestBody SourceApplySaveReqVO createReqVO) {
        return success(sourceApplyService.createSourceApply(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新资源申请")
    @PreAuthorize("@ss.hasPermission('dr:source-apply:update')")
    public CommonResult<Boolean> updateSourceApply(@Valid @RequestBody SourceApplySaveReqVO updateReqVO) {
        sourceApplyService.updateSourceApply(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除资源申请")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('dr:source-apply:delete')")
    public CommonResult<Boolean> deleteSourceApply(@RequestParam("id") Long id) {
        sourceApplyService.deleteSourceApply(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得资源申请")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('dr:source-apply:query')")
    public CommonResult<SourceApplyRespVO> getSourceApply(@RequestParam("id") Long id) {
        SourceApplyDO sourceApply = sourceApplyService.getSourceApply(id);
        SourceApplyRespVO res = BeanUtils.toBean(sourceApply, SourceApplyRespVO.class);
        List<SourceApplyDetailDO> detailList = sourceApplyDetailService.getSourceApplyDetailList(id);
        List<SourceCatalogWithSourceDataRespVO> detailList1 = sourceService.queryListByIds(detailList.stream().map(SourceApplyDetailDO::getSourceId).toList());
        res.setSourceList(BeanUtils.toBean(detailList1, SourceCatalogWithSourceDataRespVO.class));
        return success(res);
    }

    @GetMapping("/page")
    @Operation(summary = "获得资源申请分页")
    @PreAuthorize("@ss.hasPermission('dr:source-apply:query')")
    public CommonResult<PageResult<SourceApplyRespVO>> getSourceApplyPage(@Valid SourceApplyPageReqVO pageReqVO) {
        PageResult<SourceApplyDO> pageResult = sourceApplyService.getSourceApplyPage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, SourceApplyRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出资源申请 Excel")
    @PreAuthorize("@ss.hasPermission('dr:source-apply:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportSourceApplyExcel(@Valid SourceApplyPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<SourceApplyDO> list = sourceApplyService.getSourceApplyPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "资源申请.xls", "数据", SourceApplyRespVO.class,
                        BeanUtils.toBean(list, SourceApplyRespVO.class));
    }

}